package org.jdbcparser.driver;

import java.sql.Connection;
import java.sql.DriverPropertyInfo;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Logger;

public class Driver implements java.sql.Driver {

	public Connection connect(String url, Properties info) throws SQLException {
		// TODO RETURN WRAPPED CONNECTION OBJECT
		return null;
	}

	public boolean acceptsURL(String url) throws SQLException {
		
		try {
			String targetURL = DriverUtils.getTargetDriverURL(url);
			return createTargetDriver(url).acceptsURL(targetURL);
		} catch(IllegalArgumentException e) {
			return false;
		}

	}

	public DriverPropertyInfo[] getPropertyInfo(String url, Properties info)
			throws SQLException {
		String targetURL = DriverUtils.getTargetDriverURL(url);
		return createTargetDriver(url).getPropertyInfo(targetURL, info);
	}

	public int getMajorVersion() {
		return 0;
	}

	public int getMinorVersion() {
		// TODO Auto-generated method stub
		return 1;
	}

	public boolean jdbcCompliant() {
		return true;
	}

	public Logger getParentLogger() throws SQLFeatureNotSupportedException {
		throw new SQLFeatureNotSupportedException("Not supported feature");
	}

	private Driver createTargetDriver(String url) {
		Map<String, String> driverParams = DriverUtils.getParserDriverParams(url);
		String targetURL = DriverUtils.getTargetDriverURL(url);
	
		if(!driverParams.containsKey("driver")) {
			String targetDriverClass = DriverUtils.determineDatabaseDriver(targetURL);
			driverParams.put("driver", targetDriverClass);
		}
	
		Driver targetDriver = DriverUtils.createDatabaseDriver(driverParams.get("driver"));
	
		return targetDriver;
	}
}
